home *** CD-ROM | disk | FTP | other *** search
/ Amiga Tools 1 / Amiga Tools.iso / egs-tools / egs_dev-disk / egsdemos / egsexamples / other / lines.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-06-06  |  6.0 KB  |  379 lines

  1. /*
  2. *
  3. *  $ FILE     : lines.c
  4. *  $ VERSION  : 1
  5. *  $ REVISION : 2
  6. *  $ DATE     : 08-Dec-93 18:31
  7. *  $
  8. *  $ Author   : mvk
  9. *  $
  10. *
  11. *       This is a line drawing demo for the Amiga or EGS .
  12. *       For running under EGS please compiler with
  13. *       sc DEF=EGS lines.c LINK
  14. *
  15. */
  16.  
  17. #include <stdlib.h>
  18. #include <stdio.h>
  19. #include <exec/types.h>
  20. #include <exec/nodes.h>
  21. #include <exec/lists.h>
  22. #include <intuition/intuition.h>
  23. #include <graphics/text.h>
  24. #include <proto/exec.h>
  25. #include <proto/graphics.h>
  26. #include <proto/intuition.h>
  27.  
  28. USHORT wakeup; /* Wake me up for event */
  29. USHORT class; /* Intu event class */
  30. USHORT code; /* Intu event code */
  31.  
  32. #ifndef EGS
  33.  
  34. struct Window *w;
  35. struct RastPort *rp,*cdrp;
  36. struct ViewPort *vp;
  37. struct IntuiMessage *message;
  38.  
  39. #define MIX_CLOSEWINDOW CLOSEWINDOW
  40. #define MIX_NEWSIZE     NEWSIZE
  41.  
  42.  
  43.  
  44. #else
  45.  
  46. /*
  47. **   EGS Include & Variablen Stuff
  48. **
  49. **
  50. */
  51.  
  52. #include <egs/egsintui.h>
  53. #include <egs/egsgfx.h>
  54.  
  55. #include <egs/proto/egsintui.h>
  56. #include <egs/proto/egsgfx.h>
  57.  
  58. struct EG_RastPort *rp,*cdrp;
  59. struct EI_Window *w;
  60. struct EI_EIntuiMsg *message;
  61.  
  62. struct Library *EGSGfxBase;
  63. struct Library *EGSIntuiBase;
  64.  
  65.  
  66. #define MIX_CLOSEWINDOW EI_iCLOSEWINDOW
  67. #define MIX_NEWSIZE     EI_iNEWSIZE
  68.  
  69. /*
  70. */
  71.  
  72.  
  73. #endif
  74.  
  75. /* Proto */
  76.  
  77. int event(void);
  78. void crash(char *string);
  79. void exit(int i);
  80.  
  81.  
  82.  
  83.  
  84. /************************ Window Defines ***********************************/
  85. #ifndef EGS
  86.  
  87. struct NewWindow nw = {
  88.   100,100,  /* Starting corner */
  89.   300,100,  /* Width, height */
  90.   2,1,   /* detail, block pens */
  91.  MIX_CLOSEWINDOW | MIX_NEWSIZE,  /* IDCMP flags */
  92.  WINDOWDEPTH | WINDOWDRAG | WINDOWCLOSE | GIMMEZEROZERO | WINDOWSIZING,
  93.      /* Window flags */
  94.   NULL,   /* Pointer to first gadget */
  95.   NULL,   /* Pointer to checkmark */
  96.   "Nervous Lines", /* title */
  97.   NULL,   /* screen pointer */
  98.   NULL,   /* bitmap pointer */
  99.   50,50,640,400,  /* window not sized */
  100.   WBENCHSCREEN  /* type of screen */
  101.   };
  102.  
  103. #else
  104. /*
  105. **  EGS-EI_Window-Struct
  106. **
  107. */
  108.  
  109. struct EI_NewWindow nw = {
  110.    100,100,  /* Starting corner */
  111.   300,100,  /* Width, height */
  112.   50,50,640,400,  /* window not sized */
  113.   NULL,   /* screen pointer */
  114.                     /* sysGadget for the Window */
  115.  
  116.         EI_WINDOWCLOSE | EI_WINDOWSIZE |
  117.         EI_WINDOWDRAG  | EI_WINDOWBACK |
  118.         EI_WINDOWFRONT ,
  119.   NULL,   /* Pointer to first gadget */
  120.   "Nervous Lines", /* title */
  121.                     /* Flags  */
  122.         EI_WINDOWACTIVE       |
  123.         EI_SIZEBBOTTOM         |
  124.         EI_GIMMEZEROZERO,
  125.                     /* EIDCMP-FLAGS */
  126.  
  127.         MIX_CLOSEWINDOW | MIX_NEWSIZE,
  128.  
  129.         NULL,                   /* MsgPort *    */
  130.            { 3 ,1 ,0 ,2 ,1 ,3 ,1 }, /* WinColors     */
  131.         NULL,                   /* MenuPtr       */
  132.         NULL                    /* IntuiGfxPtr   */
  133. };
  134. /*
  135. */
  136.  
  137. #endif
  138.  
  139. int x[2],y[2],xd[2],yd[2],co,ox[2][16],oy[2][16],xx[128],xlim,ylim;
  140.  
  141.  
  142. /**
  143. *
  144. * main program - Since no command line data is used, and no file I/O
  145. * is performed, we do not need _main from the library. We eliminate
  146. * it by calling our main function '_main'.
  147. *
  148. **/
  149. void main()
  150. {
  151.       register short i;
  152.      register int k;
  153.      register long co;
  154.      register long j;
  155.  
  156.  
  157. /************************ Set-Up routines **********************************/
  158. /*      Let the auto init code open our libraries for us.                  */
  159.  
  160. #ifndef EGS
  161.  
  162.  w = OpenWindow(&nw);
  163.  rp = w->RPort;   /* Get the raster port pointer */
  164.  vp = &w->WScreen->ViewPort; /* Get the view port pointer */
  165.  SetAPen(rp,3);   /* Set foreground pen to white */
  166.  SetDrMd(rp,JAM1);  /* Draw with foreground pen */
  167.  
  168.  xlim = w->Width;
  169.  ylim = w->Height;
  170.  
  171.  
  172. #else
  173.  
  174. /*
  175. **  EGS - Open EGS Libraries and call some
  176. **        EGS functions
  177. **
  178. */
  179.     register long rot=0,blau=0,gruen=0;
  180.  
  181.     EGSGfxBase = OpenLibrary("egsgfx.library",0);
  182.  
  183.       if (EGSGfxBase == NULL )
  184.                  crash("Can't open egsgfx.library !");
  185.  
  186.     EGSIntuiBase  = OpenLibrary("egsintui.library",0);
  187.  
  188.       if (EGSIntuiBase == NULL )
  189.                  crash("Can't open egsintui.library !");
  190.  
  191.  
  192.     w = EI_OpenWindow(&nw);
  193.     rp = w->RPort;
  194.     EG_SetAPen(rp,0);
  195.     EG_SetDrMd(rp,0);
  196.  
  197.     xlim = w->Width;
  198.     ylim = w->Height;
  199.  
  200. /*
  201. */
  202. #endif
  203.  
  204.  for(i=0;i<2;i++)
  205.  {
  206.   x[i] = rand() % xlim + 1;
  207.   y[i] = rand() % ylim + 1;
  208.   xd[i] = rand() % 10 + 1;
  209.   yd[i] = rand() % 10 + 1;
  210.  }
  211.  co = 1;
  212.  j = 0;
  213.  do {
  214.  
  215. #ifndef EGS
  216.   SetAPen(rp,co);
  217.   Move(rp,x[0],y[0]);
  218.   Draw(rp,x[1],y[1]);
  219.   if((rand() & 127) < 2)
  220.   {
  221.    ++co;
  222.    if(co > 3) co = 1;
  223.    SetAPen(rp,co);
  224.   }
  225. #else
  226.  
  227. /*
  228. ** EGS The equal EGS functions
  229. **
  230. **
  231. */
  232.  
  233.   EG_SetAPen(rp,co);
  234.   EG_Move(rp,x[0],y[0]);
  235.   EG_Draw(rp,x[1],y[1]);
  236.  
  237.  
  238.   if (blau < 64)
  239.              blau++;
  240.         else
  241.         {
  242.              blau=0;
  243.  
  244.              if (gruen < 64)
  245.                 gruen++;
  246.              else
  247.                {
  248.               gruen=0;
  249.  
  250.               if (rot < 64)
  251.                  rot++;
  252.               else
  253.               {
  254.                   rot=0;
  255.               }
  256.  
  257.                }
  258.         }
  259.  
  260.  
  261.         co = ((rot*4)<<24) +
  262.              ((gruen*4)<<16) +
  263.              ((blau*4)<<8);
  264.  
  265.   EG_SetAPen(rp,co);
  266.  
  267. /*
  268. */
  269. #endif
  270.   for(i=0;i<2;i++)
  271.   {
  272.    ox[i][(j+10) & 15] = x[i];
  273.    oy[i][(j+10) & 15] = y[i];
  274.    x[i] += xd[i];
  275.    y[i] += yd[i];
  276.    if(x[i] < 2)
  277.    {
  278.     x[i] = 2;
  279.     xd[i] = -xd[i];
  280.    }
  281.    else if(x[i] > xlim)
  282.    {
  283.     x[i] = xlim;
  284.     xd[i] = -xd[i];
  285.    }
  286.    if(y[i] < 2)
  287.    {
  288.     y[i] = 2;
  289.     yd[i] = -yd[i];
  290.    }
  291.    else if(y[i] > ylim)
  292.    {
  293.     y[i] = ylim;
  294.     yd[i] = -yd[i];
  295.    }
  296.    if(((rand() >> 5) & 127) < 2)
  297.    {
  298.     if(xd[i] < 1)  k = 1;
  299.     xd[i] = (rand() >> 5) & 7;
  300.     if(k == 1) xd[i] = -xd[i];
  301.     k = 0;
  302.    }
  303.    if(((rand() >> 5) & 255) < 50)
  304.    {
  305.     if(yd[i] < 1) k = 1;
  306.     yd[i] = (rand() >> 5) & 7;
  307.     if(k == 1) yd[i] = -yd[i];
  308.     k = 0;
  309.    }
  310.   }
  311.   ++j;
  312.  
  313.   if(w->UserPort->mp_SigBit)
  314.   {
  315.    message = (struct EI_EIntuiMsg *)GetMsg(w->UserPort);
  316.    if(message != NULL)
  317.    {
  318.     class = message->Class;
  319.     code = message->Code;
  320.     ReplyMsg((struct Message *)message);
  321.    }
  322.   }
  323.  
  324.  } while(event());
  325.  
  326. #ifndef EGS
  327.  
  328.  CloseWindow(w);
  329. #else
  330.  EI_CloseWindow(w);
  331.     crash(NULL);
  332. /*
  333. */
  334. #endif
  335.  
  336.  
  337. }
  338.  
  339.  
  340. int event()
  341. {
  342.  switch(class)
  343.  {
  344.   case MIX_CLOSEWINDOW:
  345.    return(0);
  346.   case MIX_NEWSIZE:
  347.    xlim = w->Width;
  348.    ylim = w->Height;
  349.    return(1);
  350.  }
  351.  return(1);
  352. }
  353.  
  354.  
  355. #ifdef EGS
  356.  
  357. /*
  358. **  CloseLibs
  359. */
  360.  
  361. void crash(char *string)
  362. {
  363.     if (string != NULL)
  364.     printf("%s\n",string);
  365.  
  366.     if (EGSGfxBase != NULL)
  367.     CloseLibrary(EGSGfxBase);
  368.  
  369.     if (EGSIntuiBase != NULL)
  370.     CloseLibrary(EGSIntuiBase);
  371.  
  372.  
  373.     exit(0);
  374. }
  375.  
  376. /*
  377. */
  378. #endif
  379.